define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	var ideService = require("$UI/system/designer/webIde/common/IDEService");
	var xCloudService = ideService.getXCloudService();
	
	var Model = function(){
		this.callParent();
	};
	
	Model.prototype.getUrl = function(name){
		if(!name){
		
		}else{
			return require.toUrl(name);
		}
	};



	Model.prototype.modelParamsReceive = function(event){
		
	};

	Model.prototype.demoDataCustomRefresh = function(event){
		var data = this.getJson();
		this.comp("demoData").loadData(data);
	};

	Model.prototype.deleteDataClick = function(event){
		var row = event.bindingContext.$object;
		this.comp('demoData').deleteData(row, {
			async : true
		});
	};

	Model.prototype.demoDataCustomDelete = function(event){
		var  path = "main/jianjiedz";
		var tababeName = path.substring(path.lastIndexOf("/")+1,path.length);
		var main = path.substring(0,path.lastIndexOf("/"));
		var filePath ="service/"+main+"/init/"+tababeName+".json";
		var rows = event.deleteRows;
		var data = this.comp("demoData");
		$.each(rows, function(i, row) {
			row.data.remove(row);
		});
		var json = data.toJson({"format":"simple"}).rows;
		$.each(json,function(i,v){
			delete v.userdata;
		});
		xCloudService.saveFile({filePath:filePath,content:JSON.stringify(json)});		
	};
	
	Model.prototype.getJson = function(){
		var  path = "main/jianjiedz";
		var tababeName = path.substring(path.lastIndexOf("/")+1,path.length);
		var main = path.substring(0,path.lastIndexOf("/"));
		var filePath ="service/"+main+"/init/"+tababeName+".json";
		var fieContent = xCloudService.getFileContent({filePath : filePath});
		var data;
		if(fieContent.content){
			data = eval('('+fieContent.content+')');
		}
		return data;
	};

	Model.prototype.button1Click = function(event){
		var dilog = this.comp("windowDialog1");
		var data = this.getJson();
		dilog.open({src:require.toUrl("./editData.w"),title:'编辑数据',params:{
			data:data,
			fid:event.bindingContext.$object.val("fid")
		}});
	};


	Model.prototype.windowDialog1Received = function(event){
		this.comp('demoData').refreshData();
	};

	Model.prototype.addBtnClick = function(event){
		var dilog = this.comp("windowDialog1");
		var data = this.getJson();
		dilog.open({src:require.toUrl("./addData.w"),title:'新增数据',params:{
			data:data
		}});
	};

	Model.prototype.delAllBtnClick = function(){
		var data = this.comp('demoData');
		justep.Util.confirm("是否确定删除全部数据？",function(){
			data.deleteAllData({confirm:false});
		});
	};


	return Model;
});